package com.szholly.data.general;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.common.util.ValueConvertUtils;

/**
 * 类功能说明 定义记录行实体基类
 */
public class RowBase extends HashMap<String, Object> implements Cloneable, Serializable {

	private static final long serialVersionUID = -2265886069121726414L;

	/**
	 * 定义表的唯一值字段
	 */
	public final static String FIELD_GUID = "GUID";

	/**
	 * 定义表的版本号字段
	 */
	public final static String FIELD_VERSION = "I_VERSION";

	/**
	 * 定义表的企业名称字段
	 */
	public final static String FIELD_COMPANY = "S_COMPANY";
	
	/**
	 * 定义表的系统名称字段
	 */
	public final static String FIELD_SYSTEMNAME = "S_SYSTEMNAME";

	/**
	 * 获取或设置子表对象
	 */
	private HashMap<String, List<RowBase>> childValues;

	public RowBase() {
		setIsNew(true);
	}

	/**
	 * 获取当前列
	 */
	public String[] getColumns() {
		List<String> keys = new ArrayList<String>();
		keys.addAll(this.keySet());
		return keys.toArray(new String[keys.size()]);
	}

	/**
	 * 获取子表记录
	 * 
	 * @return
	 */
	public HashMap<String, List<RowBase>> getChildValues() {
		return this.childValues;
	}

	/**
	 * 设置子表记录
	 * 
	 * @param value
	 */
	public void setChildValues(HashMap<String, List<RowBase>> value) {
		this.childValues = value;
	}

	/**
	 * 获取字段值
	 */
	public Object getValue(GlobalDataType globalDataType, String fieldName) {
		Object obj = getValue(fieldName);
		return ValueConvertUtils.ConvertValue(globalDataType, null, obj);
	}

	/**
	 * 获取或设置字段值
	 */
	public Object getValue(String fieldName) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return null;

		String key = fieldName.toUpperCase().trim();
		return this.get(key);
	}
	
	/**
	 * 获取字段的字符值，空则返回空字符串
	 */
	public String getSafeString(String fieldName) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return "";

		String key = fieldName.toUpperCase().trim();
		Object value = this.get(key);
		if(value == null){
			return "";
		}
		else{
			return this.get(key).toString();
		}
	}
	
	/**
	 * 获取字段的值，并转成数字，如果为空则返回0
	 */
	public int getSafeInt(String fieldName) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return 0;

		String key = fieldName.toUpperCase().trim();
		Object value = this.get(key);
		int result = 0;
		if(value == null){
			return 0;
		}
		
		try {
			result = Integer.parseInt(value+"");
		} catch (Exception e) {
			return 0;
		}
		return result;
	}
	
	/**
	 * 获取字段的值，并转成双精度数字，如果为空则返回0
	 */
	public double getSafeDouble(String fieldName) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return 0;

		String key = fieldName.toUpperCase().trim();
		Object value = this.get(key);
		double result = 0;
		if(value == null){
			return 0;
		}
		
		try {
			result = Double.parseDouble(value+"");
		} catch (Exception e) {
			return 0;
		}
		return result;
	}
	
	/**
	 * 获取字段的值，并转成双精度数字
	 */
	public Double getDouble(String fieldName) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return null;

		String key = fieldName.toUpperCase().trim();
		Object value = this.get(key);
		double result = 0;
		if(value == null){
			return null;
		}
		
		try {
			result = Double.parseDouble(value+"");
		} catch (Exception e) {
			return null;
		}
		return result;
	}
	
	/**
	 * 获取日期值
	 * gislin
	 * 2016年1月18日
	 * @param fieldName
	 * @return
	 */
	public Date getDate(String fieldName){
		if (StringUtils.IsNullOrSpace(fieldName))
		{
			return null;
		}
		
		String key = fieldName.toUpperCase().trim();
		Object value = get(key);
		
		if(value == null){
			return null;
		}
		
		try {
			Date date = (Date)value;
			return date;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 设置字段值
	 */
	public void setValue(String fieldName, Object obj) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return;
		String key = fieldName.toUpperCase().trim();
		this.put(key, obj);
	}

	public static String FIELD_ISNEW = "FIELD_ISNEW";

	/**
	 * 获取当前实现是否为新对象
	 * 
	 * @return
	 */
	public boolean getIsNew() {
		boolean result = (Boolean) getValue(GlobalDataType.Boolean, FIELD_ISNEW);
		return result;
	}

	/**
	 * 设置对象是否为新对象
	 * 
	 * @param value
	 */
	public void setIsNew(boolean value) {
		setValue(FIELD_ISNEW, value);
	}

	/**
	 * 是否为系统字段
	 */
	public static boolean isSystemField(String fieldName) {
		if (StringUtils.IsNullOrSpace(fieldName))
			return false;

		fieldName = fieldName.toUpperCase().trim();

		if (fieldName.equals(FIELD_VERSION) || fieldName.equals(FIELD_GUID)
				|| fieldName.equals(FIELD_COMPANY))
			return true;
		else
			return false;
	}

	/**
	 * 转换成IQueryFilter对象
	 * 
	 * @return
	 */
	public IQueryFilter toQueryFilter() {
		String strWhere = "";
		List<GlobalParameter> paraList = new ArrayList<GlobalParameter>();
		Set<String> keys = this.keySet();
		for (String key : keys) {
			if (this.get(key) == null)
				continue;
			strWhere += key + "=@" + key + " and ";
			paraList.add(new GlobalParameter("@" + key, this.get(key)));
		}
		if (strWhere != "") {
			IQueryFilter queryFilter = new QueryFilter();
			queryFilter.setWhereString(strWhere.substring(0,
					strWhere.length() - 4));
			queryFilter.setWhereParameter(paraList
					.toArray(new GlobalParameter[paraList.size()]));
			return queryFilter;
		}
		return null;
	}

	/**
	 * 转换成IQueryFilter对象
	 * 
	 * @param pTable
	 * @return
	 */
	public IQueryFilter toQueryFilter(ITable pTable) {
		String whereString = "";
		List<GlobalParameter> whereParameters = new ArrayList<GlobalParameter>();
		Object obj = null;

		// 根据自动增值字段
		Field[] autoFields = pTable.getAutoFields();
		if (autoFields != null && autoFields.length > 0) {
			// 只取一个
			String fieldName = autoFields[0].getFieldName();
			obj = this.getValue(fieldName);
			if (obj != null) {
				whereString = fieldName + "= @" + fieldName;
				whereParameters.add(new GlobalParameter("@" + fieldName, obj));
			}
		}

		// 根据主键
		if (whereString.length() <= 0) {
			Field[] keys = pTable.getKeyNoAutoFields();
			if (keys == null || keys.length <= 0)
				throw new RuntimeException("无法更新当前数据！原因：无法确定更新主键！");

			for (Field item : keys) {
				String fieldName = item.getFieldName();
				obj = this.getValue(fieldName);
				if (obj == null)
					throw new RuntimeException("主键[" + fieldName + "]取值错误！");

				whereString += fieldName + "=@" + fieldName + " and ";
				whereParameters.add(new GlobalParameter("@" + fieldName, obj));
			}
			whereString = whereString.substring(0, whereString.length() - 4);
		}

		IQueryFilter queryFilter = new QueryFilter();
		queryFilter.setWhereString(whereString);
		queryFilter.setWhereParameter(whereParameters
				.toArray(new GlobalParameter[whereParameters.size()]));
		return queryFilter;
	}

	/**
	 * 获取值缓存数据的key
	 * 
	 * @return
	 */
	public String getKey() {
		String keyString = "";
		Set<String> keys = this.keySet();
		for (String key : keys) {
			if (get(key) == null) {
				keyString += key + "_null_";
			} else {
				keyString += key + "_" + get(key) + "_";
			}
		}
		return keyString;
	}

	/**
	 * 获取条件对象
	 * 
	 * @return
	 */
	public Object getWhereObject() {
//		BasicDBObject dbObject = new BasicDBObject(this);
		return null;
	}
}